package com.example.reporter;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import com.example.aggregator.Aggregator;
import com.example.storage.MetricsStorage;
import com.example.viewer.Viewer;

public class ConsoleReporter extends ScheduledReporter {

    private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

    public ConsoleReporter(MetricsStorage storage, Aggregator aggregator, Viewer viewer) {
        super(storage, aggregator, viewer);
        //TODO Auto-generated constructor stub
    }

    public void startReporter(long period, final double durationInSection) {
        System.out.println("startReporter period=" + period);
        // 定时任务
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {

                double endTimeInMillis = System.currentTimeMillis();
                double startTimeInMillis = endTimeInMillis - durationInSection * 1000;

                doStatAndReport(startTimeInMillis, endTimeInMillis);
            }
        }, 1, period, java.util.concurrent.TimeUnit.SECONDS);
    }

    public void finalize() {
        System.out.println("ConsoleReporter finalize");
    }
}
